<template>
    <div class="login_container">
        <!-- 登录盒子  -->
        <div class="login_box">
            <!-- 登录表单 -->
            <el-form  label-width="80px" :model="registerForm" :rules="registerFormRules" ref="registerFormRef">
                <div><h1 style="text-align: center">用户注册</h1></div>
                <el-form-item label="邮箱" prop="username">
                    <el-input v-model="registerForm.username"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="name">
                    <el-input v-model="registerForm.name"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="registerForm.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input v-model="registerForm.checkPass" type="password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="registerUser">立即创建</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            let validateCheckPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.registerForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            }
            let validateUsername = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                }
                this.$http.get("/api_user/user/check_username?username="+value).then(res=>{
                    if(!res.data.flag){
                        callback(new Error('邮箱已经注册'));
                    }else{
                        callback()
                    }
                }).catch(res=>{
                    callback(new Error('服务器连接失败'))
                })
            }
            return {
                //数据绑定
                registerForm: {
                    username: 'lvxiao',
                    password: 'admin',
                    name:'小弟弟',
                    checkPass:'admin'
                },
                //表单验证规则
                registerFormRules: {
                    username: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']},
                        { validator: validateUsername, trigger: ['blur']}
        ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        {
                            min: 4,
                            max: 15,
                            message: '密码长度在 4 到 15 个字符',
                            trigger: ['blur', 'change']
                        }
                    ],
                    checkPass: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { validator: validateCheckPass, trigger: ['blur', 'change']}
                    ],
                    name: [{ required: true, message: '请输入昵称', trigger: 'blur'},
                        {
                        min: 1,
                        max: 8,
                        message: '昵称长度在 1 到 8 个字符',
                        trigger: ['blur', 'change']
                    }]
                }
            }
        },
        //添加行为，
        methods: {
            //注册功能
            registerUser() {
                //点击注册的时候先调用validate方法验证表单内容是否有误
                this.$refs.registerFormRef.validate(async valid => {
                    //如果valid参数为true则验证通过
                    if (!valid) {
                        return
                    }
                    //发送请求进行注册
                    const { data: res } = await this.$http.post('/api_user/user/register', this.$qs.stringify(this.registerForm))
                    if (res.status !== 200) {
                        return this.$message.error('注册失败:' + res.message) //console.log("登录失败:"+res.meta.msg)
                    }

                    this.$message.success('注册成功')
                    console.log(res)
                    this.$router.push('/login')
                })
            }
        }
    }
</script>

<style lang="less" scoped>
    .login_container {
        background-color: #2b5b6b;
        height: 100%;
    }
    .login_box {
        width: 375px;
        height: 400px;
        background: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        .avatar_box {
            height: 130px;
            width: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }
    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
    .el-form-item{
        width: 90%;
    }

</style>

